package main

import "flag"
//import "strings"
import "log"
import "time"
import "demoutil"

//import "fmt"

// Distributed System
import "shardkv"

//go build shardkv_server.go
//./shardkv_server -port=9001 -peers=machine2:9001,machine3:9001

// using tcp for simplicity

var Port = flag.String("port", "0", "port number")
var Peers = flag.String("peers", "", "port number")
var Masters = flag.String("masters", "", "port number")
var Gid = flag.Int("gid", -1, "Shard Group Id")
var PauseTransaction = flag.Bool("pause-transaction", false, "pause transaction for test")

var sa *shardkv.ShardKV

func main() {
	flag.Parse()
	if *Port == "0" {
		log.Fatal("need a port number ")
	}
	if *Masters == "" {
		log.Fatal("need shardmaster ")
	}
	if *Gid == -1 {
		log.Fatal("need shardgroup id ")
	}
	servers := demoutil.ParseAddresses(*Peers)
	servers = append(servers, ":" + *Port)
	me := len(servers) - 1
	masters := demoutil.ParseAddresses(*Masters)

	//fmt.Println(servers,masters)
	sa = shardkv.StartServer(int64(*Gid), masters, servers, me)
	sa.SetTag("skv_")
	sa.Pause_transaction = *PauseTransaction
	for sa.Alive() {
		time.Sleep(100 * time.Second)
	}

}
